﻿using ASPItalia.ModelVirtualCasting.Common;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace ASPItalia.ModelVirtualCasting
{
    public sealed class ApplicationContext : IApplicationContext
    {
        private static object _sync = new object();
        private static volatile ApplicationContext _currentInstance;
        
        private readonly IUnityContainer _container;
        private readonly UnityConfigurationSection _configurationSection;

        private ApplicationContext()
        {
            _container = new UnityContainer()
                .AddNewExtension<Interception>();
            _configurationSection = Configuration.UnitySection as UnityConfigurationSection;
            if (_configurationSection.Containers.Default != null)
                _configurationSection.Configure(_container);

            #region setup cache
            _container
                .AddNewExtension<Interception>()
                .Configure<Interception>()
                .SetInterceptorFor<INewsRepository>(new InterfaceInterceptor());
            #endregion
        }

        public static ApplicationContext Current
        {
            get
            {
                if (_currentInstance.IsNull())
                {
                    lock (_sync)
                    {
                        if (_currentInstance.IsNull())
                            _currentInstance = new ApplicationContext();
                    }
                }
                return _currentInstance;
            }
        }

        public IUnityContainer Container
        {
            get { return _container; }
        }

        public ICandidateRepository Candidates
        {
            get { return Container.Resolve<ICandidateRepository>(); }
        }

        public IModelRepository Models
        {
            get { return Container.Resolve<IModelRepository>(); }
        }

        public INewsRepository News
        {
            get { return Container.Resolve<INewsRepository>(); }
        }

        public IRequestRepository Requests
        {
            get { return Container.Resolve<IRequestRepository>(); }
        }

        public ICountryRepository Countries
        {
            get { return Container.Resolve<ICountryRepository>(); }
        }

        public IJobLevelRepository JobLevels
        {
            get { return Container.Resolve<IJobLevelRepository>(); }
        }

        public IJobTypeRepository JobTypes
        {
            get { return _container.Resolve<IJobTypeRepository>(); }
        }

        public ILanguageRepository Languages
        {
            get { return Container.Resolve<ILanguageRepository>(); }
        }

        public IPersonRepository People
        {
            get { return Container.Resolve<IPersonRepository>(); }
        }

        public IUserRepository Users
        {
            get { return Container.Resolve<IUserRepository>(); }
        }

        public IUserRoleRepository UserRoles
        {
            get { return Container.Resolve<IUserRoleRepository>(); }
        }
    }
}
